package com.wyh.controller.feemanagementcontroller;



import com.wyh.common.result.Result;
import com.wyh.domain.dto.feemanagementdto.ResourceFeeDTO;
import com.wyh.domain.dto.feemanagementdto.ResourcePhoneOrNameDTO;
import com.wyh.domain.dto.gfive.LoginUser;
import com.wyh.domain.po.feemanagementpo.*;
import com.wyh.security.gfive.service.TokenService;
import com.wyh.service.feemanagementservice.IBuildingunitsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * <p>
 * 资源表 前端控制器
 * </p>
 *
 * @author ggt
 * @since 2024-05-21
 */
@RestController
@RequestMapping("/buildingunits")
@RequiredArgsConstructor
@Api(tags = "楼栋管理")
public class BuildingunitsController {

    private final IBuildingunitsService buildingUnitService;

    private final TokenService tokenService;


    @ApiOperation("下拉选取楼盘")
    @PostMapping("/selectProject")
    public Result<ResponseEntity<List<ProjectUnitNode>>> getPerject(HttpServletRequest request){
        LoginUser loginUser = tokenService.getLoginUser(request);
        String account = loginUser.getUser().getAccount();
        List<ProjectUnitNode> perjectes = buildingUnitService.getPerjectes(account);
        return Result.success(ResponseEntity.ok(perjectes));
    }

    @ApiOperation("下拉选取楼栋")
    @GetMapping("/selectBuilding")
    public Result<ResponseEntity<List<BuildingUnit>>> getBuildingUnits(@RequestParam Long perjectid){
            List<BuildingUnit> buildingUnits = buildingUnitService.getBuildingUnits(perjectid);
            return Result.success(ResponseEntity.ok(buildingUnits));
    }

    @ApiOperation("下拉选取楼层")
    @GetMapping("/selectFooler")
    public Result<ResponseEntity<List<Layeres>>> getFoolers(@RequestParam Integer buildingid){
        List<Layeres> floorIds = buildingUnitService.getFloorsBybuildingId(buildingid);
        return Result.success(ResponseEntity.ok(floorIds));
    }


    @ApiOperation("根据客户名或者客户电话筛选资源")
    @GetMapping("/selectResource")
    public Result<ResponseEntity<List<ResourcePhoneOrNameDTO>>> getResourceByPhoneORName(@RequestParam String nameOrPhone, @RequestParam Integer type, HttpServletRequest request){
        LoginUser loginUser = tokenService.getLoginUser(request);
        String account = loginUser.getUser().getAccount();
        List<ResourcePhoneOrNameDTO> resourceesList = buildingUnitService.getResourceByNameOrPhoneNull(nameOrPhone,type,account);
        return Result.success(ResponseEntity.ok(resourceesList));
    }



    @ApiOperation("选取楼盘/楼栋/楼层后获取资源")
    @GetMapping("/getResources")
    public Result<ResponseEntity<List<ResourceFeeDTO>>> getResourceDTO(
            @RequestParam(name = "projectid") Long projectid,
            @RequestParam(name = "buildingid",required = false) Integer buildingid,
            @RequestParam(name = "layerid" , required = false) Integer Layerid,
            @RequestParam(name = "resourceType") Integer resourceType) {
        if(buildingid == null&&Layerid == null){
            List<ResourceFeeDTO> resourceFeeDTOS = buildingUnitService.listResourcesByProjectId(projectid,resourceType);
            return Result.success(ResponseEntity.ok(resourceFeeDTOS));
        } else if (Layerid == null) {
            List<ResourceFeeDTO> resourceFeeDTOS = buildingUnitService.listResourcesByBuildingUtilId(buildingid, projectid, resourceType);
            return Result.success(ResponseEntity.ok(resourceFeeDTOS));
        }
        List<ResourceFeeDTO> resourceDTOS = buildingUnitService.getResourceListByBuildingUtilId(Layerid,buildingid,projectid,resourceType);
        return Result.success(ResponseEntity.ok(resourceDTOS));
    }


}
